package com.mfh.auto.create.mvvm.autocreatemvvm.utils

import com.intellij.psi.PsiDirectory
import com.intellij.psi.impl.file.PsiDirectoryFactory
import com.mfh.auto.create.mvvm.autocreatemvvm.Config
import com.mfh.auto.create.mvvm.autocreatemvvm.enums.EPlatform
import com.mfh.auto.create.mvvm.autocreatemvvm.enums.EType
import com.mfh.auto.create.mvvm.autocreatemvvm.file.hm.HMDialogFileModel
import com.mfh.auto.create.mvvm.autocreatemvvm.file.hm.HMDialogModelFileModel
import com.mfh.auto.create.mvvm.autocreatemvvm.file.hm.HMPageFileModel
import com.mfh.auto.create.mvvm.autocreatemvvm.file.hm.HMPageViewModelFileModel
import com.mfh.auto.create.mvvm.autocreatemvvm.file.v1.*

object CreateFileUtils {

    /**
     * 创建文件
     */
    fun createFile(directory: PsiDirectory) {
        println("开始创建文件")
        if (Config.name.isEmpty()) {
            println("获取名称是空的")
            return
        }

        when (Config.type) {
            EType.ACTIVITY -> {
                ActivityFileModel.createFile(directory)
                ViewModelFileModel.createFile(directory)
            }

            EType.FRAGMENT -> {
                FragmentFileModel.createFile(directory)
                ViewModelFileModel.createFile(directory)
            }

            EType.HOLDER -> {
                ViewHolderFileModel.createFile(directory)
            }

            EType.DIALOG -> {
                if (Config.platform == EPlatform.ANDROID) {
                    DialogFileModel.createFile(directory)
                } else {
                    HMDialogFileModel.createFile(directory)
                    HMDialogModelFileModel.createFile(directory)
                }
            }

            EType.PAGE -> {
                HMPageFileModel.createFile(directory)
                HMPageViewModelFileModel.createFile(directory)
            }

            else -> {}
        }

        if (Config.platform != EPlatform.HM) {
            // 创建布局文件
            LayoutFileModel.createFile(directory)
        }
    }

    /**
     * 根据文件夹获取包名
     */
    fun getPackageByDirectory(directory: PsiDirectory): String {
        val directoryFactory = PsiDirectoryFactory.getInstance(directory.project)
        return directoryFactory.getQualifiedName(directory, false)
    }
}